package com.ronghui.cash.interceptor;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.servlet.handler.HandlerInterceptorAdapter;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

public class CheckAccessTokenInterceptor extends HandlerInterceptorAdapter {

	private static Logger log = Logger.getLogger(CheckAccessTokenInterceptor.class);

	@Autowired
	RedisTemplate redisTemplate;

	@Override
	public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {

		String token = request.getParameter("token");
		if (token == null || redisTemplate.opsForValue().get("access:token:" + token) == null) {
			request.getRequestDispatcher("/errorPage401.json").forward(request, response);
			return false;
		}

		return true;

	}

}